package com.jsgt.greekfire.dao

import android.content.Context
import androidx.room.*
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.jsgt.greekfire.dao.dao.AddressDao
import com.jsgt.greekfire.dao.dao.MarketBeanDao
import com.jsgt.greekfire.dao.dao.IdentityBeanDao
import com.jsgt.greekfire.dao.dao.WalletBeanDao
import com.jsgt.greekfire.dao.model.ContactAddress
import com.jsgt.greekfire.dao.model.MarketBean
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.WalletBean

@Database(
    entities = [MarketBean::class, IdentityBean::class, WalletBean::class, ContactAddress::class],
    version = 11
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun marketBeanDao(): MarketBeanDao
    abstract fun identityBeanDao(): IdentityBeanDao
    abstract fun walletBeanDao(): WalletBeanDao
    abstract fun addressDao(): AddressDao

    companion object {
        private var instance: AppDatabase? = null
        fun getInstance(context: Context): AppDatabase {
            if (instance == null) {
                instance = Room
                    .databaseBuilder(
                        context.applicationContext,
                        AppDatabase::class.java,
                        "atr_wallet.db" //数据库名称
                    )
//                    .fallbackToDestructiveMigration()//数据库更新时删除数据重新创建
//                    .addMigrations(object : Migration(3, 4) { //从数据库版本3升级到版本4需要的策略
//                        override fun migrate(database: SupportSQLiteDatabase) {
//                            database.execSQL("CREATE TABLE wallet (id INTEGER,name TEXT, address TEXT, mnemonic TEXT, priKey TEXT, password TEXT, backup INTEGER, portrait TEXT, PRIMARY KEY(id))")
//                        }
//                    })
//                    .addMigrations(object : Migration(4, 5) {
//                        override fun migrate(database: SupportSQLiteDatabase) {
//                            database.execSQL("alter table wallet add column password_tips TEXT")
//                        }
//                    }, object : Migration(5, 6) {
//                        override fun migrate(database: SupportSQLiteDatabase) {
//                            database.execSQL("alter table wallet rename to identity")
//                        }
//                    }, object : Migration(6, 7) {
//                        override fun migrate(database: SupportSQLiteDatabase) {
//                            database.execSQL("CREATE TABLE wallet (id TEXT,name TEXT, address TEXT, mnemonic TEXT, identity_id TEXT, PRIMARY KEY(id))")
//                        }
//                    })
                    .addMigrations(object : Migration(10, 11) {
                        override fun migrate(database: SupportSQLiteDatabase) {
                            database.execSQL("CREATE TABLE address (id INTEGER NOT NULL,name TEXT NOT NULL, address TEXT NOT NULL, `key` TEXT NOT NULL, description TEXT, PRIMARY KEY(id))")
                        }
                    })
                    .allowMainThreadQueries()
                    .build()
            }
            return instance as AppDatabase
        }
    }
}